<?php
namespace Common\Model;
use Think\Model;

class NewsCategoryModel extends Model{
	

	public $_validate = array();

	protected $trueTableName = 'news_category';

	public function addCategory(){
		if($_POST['category'] == ''){
			$this->error='栏目名称不能为空';
			return false;
		}
		$category = $this->add($_POST);
		if($category){
			return true;
		}else{
			$this->error="栏目添加失败";
			return false;
		}
	}

	public function getCategoryById($id){
		$map['id'] = array('eq',$id);
		//$pid = $this->where($map)->order('id asc')->getField('pid'); //array("pid"=>array('eq',$pid))
		$category = $this->where($map)->field("category,directory,title,pid,id")->order('id')->select();
		return $category;
	}

	public function getTopCategoryList(){
		$cate = S('cate_cid');
		if($cate){
			return $cate;
		}
		$cate = $this->where(array('pid'=>0))->select();
		$idArr = array();
		foreach($cate as $k => $v){
			$temp = $this->where(array('pid'=>$v['id']))->field('id')->select();
			$temp = array_column($temp,'id');
			$temp[] = $v['id'];
			$temp = array_map('intval',$temp);
			$cate[$k]['cid'] = $temp;
		}
		S('cate_cid',$cate);
		return $cate;
	}

	public function getCategoryList(){
		$cateList = $this->select();
		$cate = new \Org\Util\Data;
		$cateList = $cate::tree($cateList,'category','id','pid');
		return $cateList;
	}
	  

	public function editCategory(){
		$id = I('get.id');
		if($_POST['category'] == ''){
			$this->error='栏目名称不能为空';
			return false;
		}
		$category = $this->where("id ='{$id}'")->save($_POST);
		if($category){
			return true;
		}else{
			$this->error="栏目更新失败";
			return false;
		}
	}

	public function getFatherCategory($cid){
		return $this->where("id = $cid")->field("category,directory,title,pid,id")->find();
	}

	public function getCategoryByPid($pid){
		return $this->where("pid = $pid")->field("category,directory,title,pid,id")->select();
	}

	public function getAllCategory(){
		return $this->select();
	}

	public function getCategoryGroPid(){
		return $this->field("id,category,pid")->group('pid,id')->select();
	}
}
